package com.ruiyian.bridge.info.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruiyian.bridge.info.domain.Mmh2oInfo;
import com.ruiyian.bridge.info.mapper.Mmh2oInfoMapper;
import com.ruiyian.bridge.info.service.Mmh2oInfoService;
import com.ruiyian.bridge.project.domain.vo.*;
import com.ruiyian.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/6/17
 * Desc: 静力沉降数据
 */
@Service
public class Mmh2oInfoServiceImpl extends ServiceImpl<Mmh2oInfoMapper, Mmh2oInfo> implements Mmh2oInfoService {
    @Autowired
    private Mmh2oInfoMapper mmh2oInfoMapper;
    /**
     * 当前时刻数据
     * @param addr
     * @param macaddr
     * @return
     */
    @Override
    public DataAnalysisVo getNowMinData(String addr, String macaddr) {
        String now = DateUtils.dateTimeNow();
        String nextDate = DateUtils.getNextDate(2, new Date(), -8, null);
        return mmh2oInfoMapper.getNowMinData(addr,macaddr,nextDate,now);
    }

    /**
     * 查询水平位移数据
     * @param bo
     * @return
     */
    @Override
    public Map<String, Object> queryDataListForSummary(Mmh2oQueryBo bo) {
        Map<String,Object> retrurnData = Maps.newHashMap();
        List<String> xAxis = Lists.newArrayList();
        List<String> legend = Lists.newArrayList();
        List<Double> ydata = Lists.newArrayList();
        List<Map<String,Object>> series = Lists.newArrayList();
        if(org.apache.commons.lang3.StringUtils.isNotBlank(bo.getSearchTime())){
            bo.setStartTime(bo.getSearchTime()+" 00:00:00");
            bo.setEndTime(bo.getSearchTime()+" 23:59:59");
        }
        List<DataAnalysisVo> list = mmh2oInfoMapper.queryDataListForSummary(bo);
		/*if (CollectionUtils.isEmpty(list)){
			return null;
		}*/
//		List<Mmh2oSummaryVo> resultList = new ArrayList<>();
        list.forEach(detail -> {
            xAxis.add(detail.getCreatTime());
            String jgValue = StringUtils.isEmpty(detail.getJgValue()) ? "0" : detail.getJgValue();
//			String mmh2o = StringUtils.isEmpty(detail.getMmh2o()) ? "0" : detail.getMmh2o();
            double baseValue = Double.parseDouble(jgValue);
            ydata.add(baseValue);
//			double monitorValue = Double.parseDouble(mmh2o);
//			resultList.add(new Mmh2oSummaryVo(detail.getCreattime(), baseValue, baseValue  ,monitorValue));
        });
        Map<String,Object> data = new HashMap<>();
        data.put("data",ydata);
        data.put("name","沉降值");
        series.add(data);
        legend.add("沉降值");
        retrurnData.put("legend",legend);
        retrurnData.put("category",xAxis);
        retrurnData.put("series",series);
        return retrurnData;
    }
    /**
     * 多维度统计
     * @param dto
     * @return
     */
    @Override
    public List<DataAnalysisVo> queryDisByDateType(DataAnalysisDto dto) {
        return mmh2oInfoMapper.queryDisByDateType(dto);
    }

    /**
     * 本地个数
     * @return
     */
    @Override
    public Long queryCount() {
        return mmh2oInfoMapper.queryCount();
    }

    /**
     * 外部个数
     * @return
     */
    @DS("bsalve")
    @Override
    public Long queryRemoteCount() {
        return mmh2oInfoMapper.queryRemoteCount();
    }

    /**
     * 同步数据
     * @param startNum
     * @param endNum
     * @return
     */
    @DS("bsalve")
    @Override
    public List<DataAnalysisVo> getDataList(Long startNum, Long endNum) {
        return mmh2oInfoMapper.getDataList(startNum, endNum);
    }

    /**
     * 根据当前时间,预警值查询数据
     * @param disLimitUpVlue
     * @param startDate
     * @return
     */
    @Override
    public List<Mmh2oInfo> queryDataAbove(Double disLimitUpVlue, String startDate) {
        return mmh2oInfoMapper.queryDataAbove(disLimitUpVlue, startDate);
    }

    @Override
    public List<DataAnalysisVo> getNowMinDataByType(DataAnalysisDto dataAnalysisDto) {
        return mmh2oInfoMapper.getNowMinDataByType(dataAnalysisDto);
    }

    @Override
    public DataAnalysisVo queryNowDisByDateType(DataAnalysisDto dataAnalysisDto) {
        return mmh2oInfoMapper.queryNowDisByDateType(dataAnalysisDto);
    }

    @Override
    public List<BridgeSummaryVo> queryPageListByBridgeList(Mmh2oDayQueryBo bo) {
        if (bo.getDateType() == null && StringUtils.isEmpty(bo.getStartTime())){
            bo.setDateType(3);
        }
        if (bo.getDateType() != null){
            bo.setStartTime(DateUtils.getBeginOfDate(bo.getDateType()));
            bo.setEndTime(DateUtils.datetimeFormat.get().format(new Date()));
        } else if ( !StringUtils.isEmpty(bo.getStartTime())){
            bo.setStartTime(bo.getStartTime() + " 00:00:00");
            bo.setEndTime(bo.getEndTime() + " 23:59:59");
        }
        return mmh2oInfoMapper.queryPageListByBridgeList(bo);
    }

    @Override
    public List<BridgeSummaryVo> queryPageListBypier(Mmh2oDayQueryBo bo) {
        if (bo.getDateType() == null && StringUtils.isEmpty(bo.getStartTime())){
            bo.setDateType(3);
        }
        if (bo.getDateType() != null){
            bo.setStartTime(DateUtils.getBeginOfDate(bo.getDateType()));
            bo.setEndTime(DateUtils.datetimeFormat.get().format(new Date()));
        } else if ( !StringUtils.isEmpty(bo.getStartTime())){
            bo.setStartTime(bo.getStartTime() + " 00:00:00");
            bo.setEndTime(bo.getEndTime() + " 23:59:59");
        }

        List<BridgeSummaryVo> list = mmh2oInfoMapper.queryPageListBypier(bo);
        return list;
    }
}
